home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / examples / chap04 / TalkativeAgent.java < prev    next >
Text File  |  1996-09-19  |  1KB  |  37 lines

  1. //
  2. // an agent talks when you click him.
  3. //
  4.  
  5. import java.util.*;
  6. import vrml.*;
  7. import vrml.node.*;
  8. import vrml.field.*;
  9.  
  10. public class TalkativeAgent extends Script{
  11.     SFTime startTalking1;
  12.     SFTime startTalking2;
  13.     Random randomNumGenerator = new Random();
  14.     
  15.     public void initialize(){
  16.         // get the reference of the event out 'startTalking1'.
  17.         startTalking1 = (SFTime)getEventOut("startTalking1");
  18.         // get the reference of the event out 'startTalking2'.
  19.         startTalking2 = (SFTime)getEventOut("startTalking2");
  20.     }
  21.     
  22.     public void processEvent(Event e){
  23.         if(e.getName().equals("invoked") == true){
  24.             double touchTime = ((ConstSFTime)e.getValue()).getValue();
  25.             
  26.             // select one of two phrases randomly.
  27.             if(randomNumGenerator.nextDouble() > 0.5){
  28.                 // start the agent talking phrase1.
  29.                 startTalking1.setValue(touchTime);
  30.             }else{
  31.                 // stop the agent talking phrase2.
  32.                 startTalking2.setValue(touchTime);
  33.             }
  34.         }
  35.     }
  36. }
  37.